<template>
  <div class="app-container">
    <!-- 数据表 -->
    <el-table :data="data" style="width: 100%" border>
      <el-table-column prop="index" label="序号" width="60" align="center">
        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
      </el-table-column>

      <el-table-column prop="avatar" label="头像" width="100" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 50px"
            :src="scope.row.avatar"
            fit="contain"
            :preview-src-list="imgarr"
          ></el-image>
        </template>
      </el-table-column>

      <el-table-column prop="nickname" label="昵称" width="150" align="center">
        <template slot-scope="scope">{{ scope.row.nickname }}</template>
      </el-table-column>

      <el-table-column prop="content" label="内容" align="center">
        <template slot-scope="scope">{{ scope.row.content }}</template>
      </el-table-column>

      <el-table-column prop="createDate" label="创建日期" align="center">
        <template slot-scope="scope">{{ scope.row.createDate }}</template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            content="删除"
            placement="top"
            :hide-after="2000"
          >
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="deleteMessage(scope.row)"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页插件 -->
    <el-pagination
      style="margin-top: 30px"
      background
      layout="prev, pager, next,total,->,sizes,jumper"
      :page-size="limit"
      :page-sizes="[5, 10, 20]"
      :total="count"
      :current-page.sync="currentPage"
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      @prev-click="prevClickHandle"
      @next-click="nextClickHandle"
    >
    </el-pagination>
  </div>
</template>

<script>
import { formateDate } from "@/utils/tools";
import { delMessage, getMessages } from "@/api/message";
import { server_url } from "@/urlConfig";
export default {
  data() {
    return {
      data: [], //存放分页获取的博客文章
      imgarr: [], //存放所有博客预览图
      limit: 5, //每页显示的博客条数
      currentPage: 1, //当前页码
      totalPages: 0, //总页码
      count: 0, //总条数 };
    };
  },
  created() {
    //创建时获取所有评论
    this.fetchData();
  },
  methods: {
    fetchData() {
      getMessages(this.currentPage, this.limit).then((res) => {
        for (let item of res.data.rows) {
          item.createDate = formateDate(item.createDate);
          item.avatar = server_url + item.avatar;
        }
        //拿到数据
        this.data = res.data.rows;
        this.count = res.data.total;
        this.totalPages = Math.ceil(this.count / this.limit);

        // 下面的 if 会在删除文章的时候可能会触发，例如最后一页只有一条数据
        // 删除之后，总页码数就会减一，当前页码数就大于了总页码数，所以要做处理

        if (this.currentPage > this.totalPage) {
          this.currentPage = this.totalPage;
          this.fetchData();
        }
      });
    },

    deleteMessage({ id }) {
      //弹出提示框询问是否删除
      this.$confirm("此操作将永久删除该评论, 是否继续?", "是否删除此评论", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //发送删除请求
          delMessage(id).then(() => {
            //重新获取所有文章数据
            this.fetchData();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //分页插件方法
    sizeChangeHandle(newLimit) {
      //重置当前页码
      this.currentPage = 1;
      this.limit = parseInt(newLimit);
      this.fetchData();
    },
    currentChangeHandle(newPage) {
      this.currentPage = parseInt(newPage);
      this.fetchData();
    },
    prevClickHandle(newPage) {
      this.currentPage = parseInt(newPage);
      // this.currentPage -= 1;
    },
    nextClickHandle(newPage) {
      this.currentPage = parseInt(newPage);
      // this.currentPage += 1;
    },
  },
};
</script>

<style></style>
